<?php
namespace Message\Controller;
use Common\Controller\MessageController;
class ColourController extends MessageController {


    /**
     * 缓存到文件
     */
    private function cachecolour(){

        $cache = S('cachecolour');

        if (!empty($cache)){
            return $cache;
        }

        $info = M('colour_template_send')->field('wxopen_template_send_id,appid,secret,touser,template_id,page,form_id')->where(array('status'=>'-1'))->order('wxopen_template_send_id asc')->select();

        $data = array();
        foreach ($info as $key=>$value){

            $data[$value['touser']] = $value;

        }

        S('cachecolour',$data,7200);

        return S('cachecolour');

    }


    /**
     * 颜色
     */
    public function sendcolour(){

    exit;
       $info = $this->cachecolour();
//        $where['status'] = '-1';
//        $where['touser'] = 'ojI_b4gCRntlXJQCEU_nQhIYFDOg';
//        $info = M('spoof_template_send')->field('wxopen_template_send_id,appid,secret,touser,template_id,page,form_id')->where($where)->limit(1)->order('wxopen_template_send_id asc')->select();

        foreach ($info as $key=>$value){


            $fdata = array(
                'keyword1'=>array(
                    'value'=>date('Y-m-d H:i:s'),
                    'color'=>'#173177',
                ),
                'keyword2'=>array(
                    'value'=>'堪比毕加索',
                    'color'=>'#ff0000',
                ),
                'keyword3'=>array(
                    'value'=>'朋友圈都在晒准考证。',
                    'color'=>'#173177',
                ),

            );
            $temp = "qVeQxBe_RQvqr2Mh9pck8Z8PNLNsKGEJDStIZBFLWjo";
            $data = array(
                'touser'=>$value['touser'],
                'template_id'=>$temp,
                'page'=>'pages/index/index',
                'form_id'=>$value['form_id'],
                'data'=>$fdata,
                'emphasis_keyword'=>'keyword2.DATA'
            );
            $res = $this->sendtemplate($value['appid'],$value['secret'],$data);
            $save = array(
                'errcode'=>$res['errcode'],
                'errmsg'=>$res['errmsg'],
                'data'=>json_encode($fdata),
                'emphasis_keyword'=>'keyword2.DATA',
            );
            echo $res['errcode'];
            if ($res['errcode']==0){
                $save['status'] = 1;
                $save['send_time'] = time();
                M('colour_template_send')->where(array('wxopen_template_send_id'=>$value['wxopen_template_send_id']))->save($save);
            }else{
                $save['status'] = 2;
                M('colour_template_send')->where(array('wxopen_template_send_id'=>$value['wxopen_template_send_id']))->save($save);
            }

        }
        echo 'ok';
        exit;
    }

}